package org.game.module.rechargebo;

import com.sojoys.artifact.core.IServer;
import com.sojoys.artifact.exception.AbstractLogicModelException;
import com.sojoys.artifact.factory.session.ISession;
import com.sojoys.artifact.manager.PropManager;
import com.sojoys.artifact.tools.ToolError;
import com.xiaoleilu.hutool.date.DateUtil;
import org.bean.AccountInfo;
import org.cfg.RechargeCfg;
import org.cfg.cache.RechargeCfgCache;
import org.error.GameErrorCode;
import org.game.constant.GameConstant;
import org.game.constant.SessionConstent;
import org.game.db.bean.MonthCard;
import org.game.db.bean.Player;
import org.game.db.bean.RechargeGiftBagRecord;
import org.game.manager.GameRechargeManager;
import org.game.msgBuilder.RechargeMsgBuilder;
import org.game.protobuf.c2s.C2SRechargeMsg;
import org.game.protobuf.s2c.S2CShareMsg;
import org.game.util.ProtoUnit;
import org.utils.RemoteUtil;
import org.utils.TimeUtil;

import java.util.List;
import java.util.UUID;

/**
 * Created by fxf on 2018-2-10.
 */
public class RechargeBOImpl implements RechargeBO {

    @Override
    public byte[] GetRechargeCfgList(ISession session, C2SRechargeMsg.GetRechargeCfg msg) throws AbstractLogicModelException {
        Player player = session.getAttr(SessionConstent.PLAYER);
        AccountInfo accountInfo = player.getPlayerProxy().getAccountInfo();
        List<RechargeCfg> cfgList = RechargeCfgCache.me().getRechargeListByChannel(accountInfo.getChannelId(), accountInfo.getChannelAppId());
        return ProtoUnit.toByte(RechargeMsgBuilder.buildGetRechargeCfgMsg(cfgList));
    }

    @Override
    public byte[] getOrderNo(ISession session, C2SRechargeMsg.GetOrderNo msg) throws AbstractLogicModelException {
        Player player = session.getAttr(SessionConstent.PLAYER);
        int goodsId = msg.getGoodsId();

        RechargeCfg cfg = RechargeCfgCache.me().getById(goodsId);
        ToolError.isAndTrue(GameErrorCode.CLIENT_PARAM_IS_ERR, "商品id错误", cfg == null);
        GameRechargeManager.checkPurchaseLimitation(player, cfg);

        String orderNo = RemoteUtil.paymentSync().getOrderNo(player.getId(), PropManager.get(GameConstant.GAME_SERVER_ID), goodsId);

//        if (IServer.getConstants().isDevMode()){
//            GameRechargeManager.testDeliveryOrderGoods(player.getId(), UUID.randomUUID().toString(),goodsId);
//            return ProtoUnit.toByte(RechargeMsgBuilder.buildGetOrderNoMsg(""));
//        }else{
//            return ProtoUnit.toByte(RechargeMsgBuilder.buildGetOrderNoMsg(orderNo));
//        }
        return ProtoUnit.toByte(RechargeMsgBuilder.buildGetOrderNoMsg(orderNo));
    }

    @Override
    public byte[] getMonthCardInfo(ISession session, C2SRechargeMsg.GetMonthCardInfo msg) throws AbstractLogicModelException {
        Player player = session.getAttr(SessionConstent.PLAYER);
        MonthCard mc = player.getMonthCardsFkPlayerId();
        return ProtoUnit.toByte(RechargeMsgBuilder.buildGetMonthCardInfoMsg(mc, S2CShareMsg.ChangeType.DEFAULT));
    }

    @Override
    public byte[] getBuyRecordInfo(ISession session, C2SRechargeMsg.GetBuyRecordInfo msg) throws AbstractLogicModelException {
        Player player = session.getAttr(SessionConstent.PLAYER);
        List<RechargeGiftBagRecord> list = player.getRechargeGiftBagRecordsFkPlayerId();
        return ProtoUnit.toByte(RechargeMsgBuilder.buildGetBuyRecordInfoMsg(list, S2CShareMsg.ChangeType.DEFAULT));
    }
}
